package dao;

import entity.ClassDay;
import entity.ClassGroup;
import entity.IEntity;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaQuery;

/**
 *
 * @author Alexander
 */
public class ClassDayDAO implements IDAO {

    @Override
    public void persist(IEntity entity, EntityManager em) {
        em.persist(entity);
    }

    @Override
    public IEntity find(Object id, EntityManager em) {
        return (ClassDay) em.find(ClassDay.class, (int) id);
    }

    @Override
    public void update(IEntity entity, EntityManager em) {
        em.merge(entity);
    }

    @Override
    public void delete(Object id, EntityManager em) {
        ClassDay dia = (ClassDay) em.getReference(ClassDay.class, (int) id);
        em.remove(dia);
    }

    @Override
    public List<IEntity> getList(EntityManager em) {
        CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
        cq.select(cq.from(ClassDay.class));
        Query q = em.createQuery(cq);
        return q.getResultList();
    }

    public List<ClassDay> searchByGroup(ClassGroup group, EntityManager em) {
        List<ClassDay> days = new ArrayList<>();
        Query q;

        q = em.createQuery("SELECT m FROM ClassDay m "
                + "WHERE m.group = :group").setParameter("group", group);

        try {
            days = q.getResultList();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return days;
    }
}
